package com.zdsoft.blp.gongyong.linliFragment;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;

/**
 * Created by Administrator on 2016/8/24.
 */
public  class LinLiView extends FrameLayout{

    private TextView tv_left;
    private TextView tv_right;
    private ViewPager vp;
    private RelativeLayout.LayoutParams lp;
    private int currentPage;//当前页面
    private View line;
    private int screenWidth;
    private  boolean isChack;
    private int huang,black;

    public LinLiView(Context context) {
        super(context);
        init(context);
    }

    public LinLiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LinLiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater.from(context).inflate(R.layout.linli_fragment,this,true);
        tv_left = (TextView) findViewById(R.id.tv_left);
        tv_right = (TextView) findViewById(R.id.tv_right);
        vp = (ViewPager) findViewById(R.id.vp);
        line = findViewById(R.id.line);
        huang = getResources().getColor(R.color.llq_huang);
        black = getResources().getColor(R.color.black);
        //初始化颜色
        tv_left.setTextColor(huang);
        tv_right.setTextColor(black);
        isChack = true;
        tv_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_left.setTextColor(huang);
                tv_right.setTextColor(black);
                isChack = true;
                vp.setCurrentItem(0);
            }
        });
        tv_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_left.setTextColor(black);
                tv_right.setTextColor(huang);
                isChack = false;
                vp.setCurrentItem(1);
            }
        });
        //实例化动画
        initImage(context);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (currentPage == 0 && position == 0) {// 0-->1
                    lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 2) + currentPage
                            * (screenWidth / 2));
                }else if (currentPage == 1 && position == 0) {// 1-->0
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 2) + currentPage
                            * (screenWidth / 2));
                }
                line.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                currentPage =position;
                if(position==0){
                    tv_left.setTextColor(huang);
                    tv_right.setTextColor(black);
                    isChack=true;
                }else if(position==1) {
                    tv_left.setTextColor(black);
                    tv_right.setTextColor(huang);
                    isChack=false;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void initImage(Context context) {
        //获取屏幕的参数
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)context).getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        //int height=metrics.heightPixels;//屏幕的高度
        screenWidth = metrics.widthPixels;//获取屏幕的宽
        //获取这条线的参数
        lp = (RelativeLayout.LayoutParams) line.getLayoutParams();
        //给线的宽赋值，屏幕宽的1/4
        lp.width = screenWidth / 2;
        //给线设置改过宽的布局参数
        line.setLayoutParams(lp);

    }

    public ViewPager getVp() {
        return vp;
    }

    public TextView getTv_left() {
        return tv_left;
    }

    public void setTv_left(TextView tv_left) {
        this.tv_left = tv_left;
    }

    public TextView getTv_right() {
        return tv_right;
    }

    public void setTv_right(TextView tv_right) {
        this.tv_right = tv_right;
    }

    public boolean isChack() {
        return isChack;
    }

    public void setChack(boolean chack) {
        isChack = chack;
    }
}
